package com.great.controller;

import com.github.pagehelper.PageInfo;
import com.great.domain.Cate;
import com.great.service.CateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/cate")
public class CateController {

    @Autowired
    private CateService cateService;

    @RequestMapping("/findAllCate")
    public ModelAndView findAllCate(@RequestParam(name = "page", required = true,defaultValue = "1") int page,@RequestParam(name = "size",required = true,defaultValue = "10") int size){
        ModelAndView mv = new ModelAndView();
        List<Cate> cate = cateService.findAllCate(page,size);
        PageInfo pageInfo = new PageInfo(cate);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("manage/cate-list");
        return mv;
    }

    @RequestMapping("/cateadd")
    public ModelAndView cateadd(){
        ModelAndView mv = new ModelAndView();
        List<Cate> cate = cateService.findAllParentCate();
        mv.addObject("cateList",cate);
        mv.setViewName("manage/cate-add");
        return mv;
    }

    @RequestMapping("/add")
    public String add(Cate cate){
        cateService.addcate(cate);
        return "redirect:/cate/findAllCate";
    }

    @RequestMapping("/catechange")
    public ModelAndView catechange(Integer cate_id){
        ModelAndView mv = new ModelAndView();
        Cate cate = cateService.findById(cate_id);
        List<Cate> cates = cateService.findAllParentCate();
        mv.addObject("cateList",cates);
        mv.addObject("cate",cate);
        mv.setViewName("manage/cate-change");
        return mv;
    }

    @RequestMapping("change")
    public String change(Cate cate){
        cateService.updatecate(cate);
        return "redirect:/cate/findAllCate";
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        cateService.delete(id);
        return "redirect:/cate/findAllCate";
    }
}
